<template>
  <!-- template只能由一个div标签 -->
  <div>
    <div>
      <el-input
          class="addPostInput"
          size="small"
          placeholder="请输入内容"
          prefix-icon="el-icon-plus"
          v-model="pos.name">
      </el-input>
      <el-button icon="el-icon-plus" size="small" type="primary" @click="addPos">添加</el-button>
    </div>
    <div class="postManaTable">
      <el-table
          ref="multipleTable"
          :data="positions"
          border
          size="small"
          stripe
          style="width: 55%"
          @selection-change="handleSelectionChange"><!-- 这里是一个点击对话框的话框的回调函数 -->
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职位名称"
            width="180">
        </el-table-column>
        <el-table-column
            prop="createDate"
            width="200"
            label="创建时间">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button style="margin-top: 20px" size="small" type="danger" :disabled="multipleSelection.length==0" @click="deleteMana">删除已选择</el-button>
      <el-button style="margin-top: 20px" size="small" type="danger" @click="toggleSelection()">取消选择</el-button>
    </div>
    <div>
      <el-dialog
          title="修改职位"
          :visible.sync="dialogVisible"
          width="30%">
        <div>
          <!-- 对话框中的输入框 -->
          <el-tag>职位名称</el-tag>
          <el-input class="updatePosInput" size="small" v-model="updatePos.name"></el-input>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button size="small" @click="dialogVisible = false">取 消</el-button>
          <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "PosMana",
  data(){
    return {
      pos: {
        name:""
      },
      updatePos: {
        name:""
      },
      dialogVisible : false,
      positions: [],
      //当前已经选中的项的一给数组
      multipleSelection: []
    }
  },
  //组件挂载之前的时候，就去加载全部数据
  beforeMount() {
    this.initPosition()
  },
  methods: {
    initPosition(){
      this.getRequest("/system/basic/pos/").then(resp => {
        if(resp){
         this.positions = resp;//拿到对应的数据后赋值给他
        }
      })
    },
    //修改操作
    handleEdit(index, row) {
      //第一种写法
      Object.assign(this.updatePos,row);//进行数据拷贝
      //第二种写法()
      //this.updatePos = {...this.updatePos,...row};
      //console.log(this.updatePos);
      //console.log(row);
      //console.log(this2);
      this.dialogVisible = true;
    },
    //去做更新操作
    doUpdate(){
      this.putRequest("/system/basic/pos/",this.updatePos).then(resp => {
          if(resp){
            this.initPosition();
            this.updatePos.name = "";
            this.dialogVisible = false;
          }
      })
    },
    //删除操作
    handleDelete(index, row) {
      this.$confirm('此操作将删除【'+row.name+'】该职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest("/system/basic/pos/"+row.id).then(resp => {
          //重新刷新表格
          this.initPosition();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //添加操作
    addPos(){
      if(this.pos.name==""){
        this.$message({
          message: '请输入要添加的职位',
          type: 'warning'
        });
      } else {
        //注意这里传递的是JSON 数据而不是一个"name"属性，所以我么传递的是一个对象
        this.postRequest("/system/basic/pos/",this.pos).then(resp => {
          //重新刷新表格
          this.initPosition();
        })
        this.pos.name= "";
      }
    },
    //
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },

    /**
     * 点击对话框出发的事件，
     * 这个事件有个参数，我把每点击的参数传到这个数组中去
     * @param val 【被点击这一行的的素有数据】
     */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //批量删除
    deleteMana(){
      this.$confirm('此操作将删除【'+this.multipleSelection.length+'】条数据, 是否继续?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          let newMultipleSelection = "?";
          this.multipleSelection.forEach((val) => {
            newMultipleSelection += "ids=" +val.id + "&";
          })
          this.deleteRequest("/system/basic/pos/"+newMultipleSelection).then(resp => {
            //重新刷新表格
            this.initPosition();
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style>
  .addPostInput{
    width: 300px;
    margin-right: 10px;

  }
  .postManaTable{
    margin-top: 20px;
  }
  .updatePosInput{
    width: 200px;
    margin-left: 10px;
  }
</style>
